<!-- 绩效-基础设置-能力素质指标库 -->
<template>
  <ContentCard>
    <template #header>
      <pageHeaderNew :title="title" :breadCrumb="breadCrumb" showBack>
      </pageHeaderNew>
    </template>
    <ConditionBox :margin-bottom="20">
      <template #left>
        <a-input-search
          :style="{ width: '220px' }"
          placeholder="名称"
          search-button
          @search="refresh"
          @press-enter="refresh"
          v-model="params.name"
        />
      </template>
      <template #right>
        <a-button type="primary" @click="add(null)">新增指标</a-button>
      </template>
    </ConditionBox>
    <table-turn :table-data="getTableData" ref="tableRef">
      <a-table-column :width="80">
        <template #title> 序号 </template>
        <template #cell="{ rowIndex }">
          {{ rowIndex + 1 }}
        </template>
      </a-table-column>
      <a-table-column :width="200" data-index="name" title="指标名称">
      </a-table-column>
      <a-table-column :width="200" data-index="definition" title="指标定义">
      </a-table-column>
      <a-table-column
        :width="200"
        data-index=""
        title="评分标准"
        :headerCellStyle="{
          height: '30px'
        }"
      >
        <a-table-column
          :width="200"
          data-index="levelA"
          title="优秀A"
          :headerCellStyle="{
            height: '30px'
          }"
        >
        </a-table-column>
        <a-table-column
          :width="200"
          data-index="levelB"
          title="良好B"
          :headerCellStyle="{
            height: '30px'
          }"
        >
        </a-table-column>
        <a-table-column
          :width="200"
          data-index="levelC"
          title="合格C"
          :headerCellStyle="{
            height: '30px'
          }"
        >
        </a-table-column>
        <a-table-column
          :width="200"
          data-index="levelD"
          title="较差D"
          :headerCellStyle="{
            height: '30px'
          }"
        >
        </a-table-column>
        <a-table-column
          :width="200"
          data-index="levelE"
          title="差E"
          :headerCellStyle="{
            height: '30px'
          }"
        >
        </a-table-column>
      </a-table-column>
      <a-table-column
        :width="90"
        data-index="status"
        title="启用状态"
        align="center"
      >
        <template #cell="{ record }">
          <a-switch
            v-model="record.status"
            :checked-value="1"
            :unchecked-value="2"
            @change="stateChange(record)"
          ></a-switch>
        </template>
      </a-table-column>
      <a-table-column
        :width="120"
        data-index=""
        title="操作"
        fixed="right"
        align="center"
      >
        <template #cell="{ record }">
          <a-tooltip content="编辑">
            <IconFont
              type="icon-edit"
              :size="26"
              :style="{
                marginRight: '10px',
                cursor: 'pointer'
              }"
              @click="goEdit(record)"
            />
          </a-tooltip>
          <a-tooltip content="删除" position="left">
            <a-popconfirm content="是否确认删除该条数据?" @ok="del(record.id)">
              <icon-font
                type="icon-delete"
                :size="26"
                :style="{
                  cursor: 'pointer',
                  marginRight: '10px'
                }"
              />
            </a-popconfirm>
          </a-tooltip>
        </template>
      </a-table-column>
    </table-turn>
  </ContentCard>
  <AddIndex
    v-model:visible="modalVisible"
    @indexEmit="indexEmit"
    :data="currData"
  ></AddIndex>
</template>

<script lang="ts" setup>
import {
  DelBpiIndex,
  GetBpiIndexList,
  PostBpiIndex,
  PutBpiIndex
} from '@/apis/management/performance/baseSetting/template';
import { BpiIndex } from '@/apis/management/performance/baseSetting/template/type';
import { Message } from '@arco-design/web-vue';
import AddIndex from './Components/AddIndex/index.vue';

const router = useRouter();
const title = '能力素质指标库';

const breadCrumb: string[] = ['人事', '基础设置', '指标库', '能力素质指标库'];

let modalVisible = ref(false); // 判断新增弹出框是否打开
let currData = ref<BpiIndex>(); // 储存当前选中数据
let tableRef = ref();

// 新增
const add = (record: any) => {
  reset();
  modalVisible.value = true;
};
/**
 * 重置当前数据
 */
const reset = () => {
  currData.value = {
    name: '',
    definition: '',
    performanceAbilityQualityIndexLibraryScoringStandard: [
      { type: 1, value: void 0 },
      { type: 2, value: void 0 },
      { type: 3, value: void 0 },
      { type: 4, value: void 0 },
      { type: 5, value: void 0 }
    ],
    status: 1
  };
};
const params = reactive({ name: '' });
// 获取表格数据
const getTableData = async (pagination: any) => {
  try {
    const res = await GetBpiIndexList({ ...params, ...pagination });
    res.content.forEach((item: any) => {
      item.performanceAbilityQualityIndexLibraryScoringStandard.forEach(
        (ele: any) => {
          if (ele.type === 1) {
            item.levelA = ele.value;
          }
          if (ele.type === 2) {
            item.levelB = ele.value;
          }
          if (ele.type === 3) {
            item.levelC = ele.value;
          }
          if (ele.type === 4) {
            item.levelD = ele.value;
          }
          if (ele.type === 5) {
            item.levelE = ele.value;
          }
        }
      );
    });
    return Promise.resolve(res);
  } catch (error: unknown) {
    console.log(error);
  }
};
// 修改状态
const stateChange = async (data: BpiIndex) => {
  await PutBpiIndex(data);
  Message.success('修改成功');
};
// 编辑
const goEdit = (record: any) => {
  console.log(record);
  currData.value = record;
  modalVisible.value = true;
};

// 删除某一行
const del = async (id: number) => {
  try {
    await DelBpiIndex({ ids: [id] });
    refresh();
  } catch (error: unknown) {
    console.log(error);
  }
};

// 弹窗自定义事件
const indexEmit = async (data: BpiIndex) => {
  if (data.id) {
    await PutBpiIndex(data);
  } else {
    await PostBpiIndex(data);
  }
  refresh();
};

// 刷新表格数据
const refresh = () => {
  tableRef.value.loadData();
};
</script>

<style scoped lang="less"></style>
